<?php

use com\gdl\kq\interfaces\PublicAreaServiceIf;
use com\gdl\common\dataobject\common\ResultDO;
use com\gdl\kq\dataobject\publicarea\PublicAreaDTO;


class api_PublicAreaService extends api_Abstract implements PublicAreaServiceIf
{
	
	//公共区域列表
	public function getPublicAreaList(PublicAreaDTO $publicareaDTO)
    {
		$result = new ResultDO();
		$data      = $publicareaDTO->data ? $publicareaDTO->data : [];
		$id        = $publicareaDTO->id ? $publicareaDTO->id : 0;//公共区域ID
		$platformId = $publicareaDTO->platformId ? $publicareaDTO->platformId : 0;//平台ID

				
	    try {
	        $adminReg = $data['adminReg'];
            $condition = [];
                if ($id){
                    $condition['id'] = $id;
                    $apppublicareaModel = new model_attendance_apppublicarea();
                    $res = $apppublicareaModel->select($condition)->items;
                    $result->data = $res;
                    if(!empty($res[0])){
                        $result->code = 1;
                    } else {
                        $result->code = 0;
                    }

                    $result->success = true;
                }else{
                    $condition['platformId'] = $platformId;

                    $apppublicareaModel = new model_attendance_apppublicarea();
                    $res = $apppublicareaModel->select($condition)->items;
                    $result->data = $res;
                    if(!empty($res[0])){
                        $result->code = 1;
                    } else {
                        $result->code = 0;
                    }
                    $result->success = true;
                }
            } catch (Exception $e) {
                $result->success = false;
                $result->code = $e->getCode();
                $result->message = $e->getMessage();
            }
            return $result;
	}


    //添加公共区域
    public function addPublicArea(PublicAreaDTO $publicareaDTO)
    {
        $result = new ResultDO();
        $data      = $publicareaDTO->data ? $publicareaDTO->data : [];
        $platformId = $publicareaDTO->platformId ? $publicareaDTO->platformId : 0;//平台ID


        try {
            $apppublicareaModel = new model_attendance_apppublicarea();
            //查询公共区域名是否重复
                $name = $apppublicareaModel->selectOne(['platformId'=>$platformId,'name'=>$data['name']],'id,title');
                if ($name){
                    $result->code = 0;
                    $result->message = "公共区域名:（{$name['title']}）重复，请换一个区域名！";
                }else{
                    $id = $apppublicareaModel->insert($data);
                    $result->data = [[$id]];
                    if(!empty($id)){
                        $result->code = 1;
                        $result->message = "区域添加成功！";
                    } else {
                        $result->code = 0;
                        $result->message = "区域添加失败！";
                    }
                }
            $result->success = true;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
    }

    //编辑公共区域
    public function editPublicArea(PublicAreaDTO $publicareaDTO){
        $data = $publicareaDTO->data ? gdl_lib_BaseUtils::getStr($publicareaDTO->data) : "";
        $id = $publicareaDTO->id ? gdl_lib_BaseUtils::getStr($publicareaDTO->id) : 0;

        #实例化返回
        $result = new ResultDO();
        $result->success = true;
        $result->code = 0;
        $result->data = [];

        #验证
        if(empty($data) || empty($id)){
            $result->message = "缺少必要参数";
            return $result;
        }

        #处理data

        #业务
        try{

            $publicareaDTOModel = new model_attendance_apppublicarea();
            #验证

            #插入数据
            $ret = $publicareaDTOModel->update(['id'=>$id],$data);
            if($ret){
                $result->code = 1;
                $result->message = "编辑成功";
            }else{
                $result->code = 0;
                $result->message = "编辑异常";
            }
        } catch (Exception $e) {
            $result->success = true;
            $result->code = 0;
            $result->message = $e->getMessage();
        }

        return $result;
    }

    //删除公共区域
    public function deletePublicArea(PublicAreaDTO $publicareaDTO){
        $id = $publicareaDTO->id ? gdl_lib_BaseUtils::getStr($publicareaDTO->id) : 0;
        $data = $publicareaDTO->data ? gdl_lib_BaseUtils::getStr($publicareaDTO->data) : [];
        $result = new ResultDO();
        $result->success = true;
        $result->message = "您已删除，请勿重复操作";
        $result->code = 400;
        $result->data = [];
//        if(empty($platformId)){
//            $result->message = "您还没有选择平台";
//            return $result;
//        }
        $type = $data['type'];
        $platformId = $data['platformId'];
        if(empty($id) && $type==1){
            $result->message = "参数错误";
            return $result;
        }
        try{
            $publicareaDTOModel = new model_attendance_apppublicarea();
            if ($id){
                $where = " id = {$id} ";
            }else{
                $where = " platformId = {$platformId} ";
            }
            $res = $publicareaDTOModel->delete($where);
            if($res){
                $result->code = 200;
                $result->message = "已删除";
            }
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
    }

}

